home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Contributions / Haage_&_Partner / Storm-Projects / Example_v37 / ARexx / mouse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-23  |  5.1 KB  |  248 lines

  1. /*
  2.  * mouse.c v1.0 by Chris Ludwig
  3.  */
  4.  
  5. #include    <exec/types.h>
  6.  
  7. #include    <libraries/dos.h>
  8. #include    <libraries/dosextens.h>
  9.  
  10. #include    <devices/input.h>
  11. #include    <devices/inputevent.h>
  12.  
  13. #include    <clib/rexxsyslib_protos.h>
  14. #include    <clib/alib_protos.h>
  15. #include    <clib/exec_protos.h>
  16. #include    <clib/dos_protos.h>
  17.  
  18. #include    <rexx/storage.h>
  19. #include    <rexx/rxslib.h>
  20.  
  21. #include <stdlib.h>
  22. #include    <stdio.h>
  23. #include    <string.h>
  24.  
  25. #include    "SimpleRexx.h"
  26.  
  27. char *stptok(char *,char *,int,char *);
  28. int stcd_i(char *,int *);
  29.  
  30.  
  31. /* input.device stuff */
  32. struct MsgPort *inputdevport;
  33. struct InputEvent phony;
  34. struct IOStdReq *inputrequest;
  35. BYTE inputopenerror;
  36.  
  37. AREXXCONTEXT    RexxStuff;
  38.  
  39. void Quit(char whytext[],UBYTE level)
  40. {
  41.     if (!inputopenerror) CloseDevice((struct IORequest *) inputrequest);
  42.  
  43.     if (inputrequest) DeleteStdIO(inputrequest);
  44.  
  45.     if (inputdevport) DeletePort(inputdevport);
  46.  
  47.     FreeARexx(RexxStuff);
  48.  
  49.     printf("%s\n",whytext);
  50.     Exit(level);
  51. }
  52.  
  53.  
  54. /*
  55.  * Lattice control-c stop...
  56.  */
  57. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL/C handling */
  58. int chkabort(void) { return(0); }  /* really */
  59.  
  60.  
  61. void click(UWORD code)
  62. {
  63. printf("in click()\n");
  64.  
  65.     phony.ie_NextEvent = NULL;
  66.     phony.ie_Class = IECLASS_RAWMOUSE;
  67.     phony.ie_TimeStamp.tv_secs = 0;
  68.     phony.ie_TimeStamp.tv_micro = 0;
  69.     phony.ie_Code = code;                    /* button down */
  70.     phony.ie_Qualifier = 0;
  71.     phony.ie_X = 0;
  72.     phony.ie_Y = 0;
  73.  
  74.     inputrequest -> io_Command = IND_WRITEEVENT;
  75.     inputrequest -> io_Flags = 0;
  76.     inputrequest -> io_Length = sizeof(struct InputEvent);
  77.     inputrequest -> io_Data = (APTR)&phony;
  78.  
  79.     DoIO((struct IORequest *)inputrequest);
  80.  
  81.  
  82.     phony.ie_NextEvent = NULL;
  83.     phony.ie_Class = IECLASS_RAWMOUSE;
  84.     phony.ie_TimeStamp.tv_secs = 0;
  85.     phony.ie_TimeStamp.tv_micro = 0;
  86.     phony.ie_Code = code | IECODE_UP_PREFIX;    /* button up */
  87.     phony.ie_Qualifier = 0;
  88.     phony.ie_X = 0;
  89.     phony.ie_Y = 0;
  90.  
  91.     inputrequest -> io_Command = IND_WRITEEVENT;
  92.     inputrequest -> io_Flags = 0;
  93.     inputrequest -> io_Length = sizeof(struct InputEvent);
  94.     inputrequest -> io_Data = (APTR)&phony;
  95.  
  96.     DoIO((struct IORequest *)inputrequest);
  97. }
  98.  
  99.  
  100. void move(LONG mousex, LONG mousey)
  101. {
  102.     /* send phony mouse message to input stream */
  103.  
  104.     phony.ie_NextEvent = NULL;
  105.     phony.ie_Class = IECLASS_POINTERPOS;
  106.     phony.ie_TimeStamp.tv_secs = 0;
  107.     phony.ie_TimeStamp.tv_micro = 0;
  108.     phony.ie_Code = 0;
  109.     phony.ie_Qualifier = 0;
  110.     phony.ie_X = mousex;
  111.     phony.ie_Y = mousey;
  112.  
  113.     inputrequest -> io_Command = IND_WRITEEVENT;
  114.     inputrequest -> io_Flags = 0;
  115.     inputrequest -> io_Length = sizeof(struct InputEvent);
  116.     inputrequest -> io_Data = (APTR)&phony;
  117.  
  118.     DoIO((struct IORequest *)inputrequest);
  119. }
  120.  
  121.  
  122. /*
  123.  * A *VERY* simple and simple-minded example of using the SimpleRexx.c code.
  124.  *
  125.  *
  126.  * Note: You will want to RUN this program or have another shell available such
  127.  *       that you can still have access to ARexx...
  128.  */
  129. void main(int argc,char *argv[])
  130. {
  131. short    loopflag=TRUE;
  132. ULONG    signals;
  133.  
  134. int x, y, length;
  135.  
  136.     if ((inputdevport=CreatePort(0,0)) == NULL)
  137.         Quit("Couldn't create a port for input.device",25);
  138.  
  139.  
  140.     if ((inputrequest=CreateStdIO(inputdevport)) == NULL)
  141.         Quit("Couldn't create request block for input device",25);
  142.  
  143.  
  144.     if ((inputopenerror=OpenDevice("input.device",0,(struct IORequest *)inputrequest,0)) != 0)
  145.         Quit("Couldn't open input.device",25);
  146.  
  147.     /*
  148.      * Note that SimpleRexx is set up such that you do not
  149.      * need to check for an error to initialize your REXX port
  150.      * This is so your application could run without REXX...
  151.      */
  152.     RexxStuff=InitARexx("mouse","mouse");
  153.  
  154.     if (argc)
  155.     {
  156.         if (RexxStuff) printf("Send commands to port %s\n",ARexxName(RexxStuff));
  157.         else printf("ARexx is not available\n");
  158.     }
  159.  
  160.     while (loopflag)
  161.     {
  162.         signals=ARexxSignal(RexxStuff);
  163.  
  164.         if (signals)
  165.         {
  166.             struct    RexxMsg        *rmsg;
  167.  
  168.             signals=Wait(signals);
  169.  
  170.             /*
  171.              * Process the ARexx messages...
  172.              */
  173.             while (rmsg=GetARexxMsg(RexxStuff))
  174.             {
  175.             char    cBuf[24];
  176.             char    *nextchar;
  177.             char    *error=NULL;
  178.             char    *result=NULL;
  179.             long    errlevel=0;
  180.  
  181.                 nextchar=stptok(ARG0(rmsg),cBuf,24," ,");
  182.                 if (*nextchar) nextchar++;
  183.  
  184.                 if (!stricmp("CLICK",cBuf))
  185.                 {
  186.                     nextchar=stptok(nextchar,cBuf,24," ,");
  187.                     if (*nextchar) nextchar++;
  188.  
  189.                     if (!stricmp("LEFT",cBuf))
  190.                     {
  191.                         click(IECODE_LBUTTON);
  192.                     }
  193.                     else if (!stricmp("MIDDLE",cBuf))
  194.                     {
  195.                         click(IECODE_MBUTTON);
  196.                     }
  197.                     else if (!stricmp("RIGHT",cBuf))
  198.                     {
  199.                         click(IECODE_RBUTTON);
  200.                     }
  201.                     else
  202.                     {
  203.                         /* Default to left button */
  204.                         click(IECODE_LBUTTON);
  205.                     }
  206.                 }
  207.                 else if (!stricmp("MOVE",cBuf))
  208.                 {
  209.                     nextchar=stptok(nextchar,cBuf,24," ,");
  210.                     if (*nextchar) nextchar++;
  211.  
  212.                     length=stcd_i(cBuf,&x);
  213.  
  214.                     nextchar=stptok(nextchar,cBuf,24," ,");
  215.                     if (*nextchar) nextchar++;
  216.  
  217.                     length=stcd_i(cBuf,&y);
  218.  
  219.  
  220.                     move((LONG) x,(LONG) y);
  221.                 }
  222.                 else if (!stricmp("VERSION",cBuf))
  223.                 {
  224.                     result="mouse v1.0";
  225.                 }
  226.                 else if (!stricmp("QUIT",cBuf))
  227.                 {
  228.                     loopflag=FALSE;
  229.                 }
  230.                 else
  231.                 {
  232.                     error="Unknown command";
  233.                     errlevel=20;
  234.                 }
  235.  
  236.                 if (error)
  237.                 {
  238.                     SetARexxLastError(RexxStuff,rmsg,error);
  239.                 }
  240.                 ReplyARexxMsg(RexxStuff,rmsg,result,errlevel);
  241.             }
  242.  
  243.         }
  244.         else loopflag=FALSE;
  245.     }
  246.     Quit("QUIT command received.\n",0);
  247. }
  248.